package com.gsit.orc.controller;

import com.gsit.orc.util.MediaPlayerUtil;
import com.thoughtworks.selenium.DefaultSelenium;

public class MainController implements Controller {
	
	private DefaultSelenium selenium;
	
	private String RECEIPT_STRING = "0 unprocessed receipts";
	
	private String STATEMENT_STRING = "0 unprocessed statements";
	
	private static MediaPlayerUtil mediaPlayerUtil;
	
	
	
	
	public MainController(DefaultSelenium selenium) {
		super();
		this.selenium = selenium;
		mediaPlayerUtil = new MediaPlayerUtil();
	}

	@Override
	public void execute() throws Exception {
        
        selenium.click("link=Home"); 
        selenium.waitForPageToLoad("30000"); 
        
        
        String str = selenium.getText("//div[@id='MainContent_TimedPanel2']/table/tbody/tr/td");
        
        if(!str.contains(RECEIPT_STRING)){
        	System.out.println(">>>>>>>>>>>>>>>> NEW RECEIPT FOUND >>>>>>>>>>>>>>>>");
        	this.callPlayer();
        	
        }else{
        	System.out.println(">>>>>>>>>>>>>>>> NO RECEIPT FOUND >>>>>>>>>>>>>>>>");
        	this.callPlayer();
        }
        
        
        str = selenium.getText("//div[@id='MainContent_TimedPanel2']/table[2]/tbody/tr/td");
         
        if(!str.contains(STATEMENT_STRING)){
        	System.out.println(">>>>>>>>>>>>>>>> NEW STATMENT FOUND >>>>>>>>>>>>>>>>");
        }else{
        	System.out.println(">>>>>>>>>>>>>>>> NO STATMENT FOUND >>>>>>>>>>>>>>>>");
        	this.callPlayer();
        }
        
        //Thread.sleep(5000); 

	}
	
	private void callPlayer() throws Exception{
		mediaPlayerUtil.createAndRun();
	}

}
